package org.me.SendSMS;

import org.me.SendSMS.utli.GlobalVariable;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class SelectDateActivity extends Activity implements OnClickListener {
	
	Button btnSetDate, btnCancelDate;
	Button btnCongDate, btnTruDate, btnCongMonth, btnTruMonth,btnCongYear, btnTruYear;
	EditText editDate, editMonth, editYear;
	Context mContext;
	
	String date = "";
	String month = "";
	String year = "";
	private GoogleAnalyticsTracker tracker;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		 tracker = GoogleAnalyticsTracker.getInstance();
	     // Start the tracker in manual dispatch mode...
	    tracker.startNewSession("UA-35081259-1", getApplication());

		setContentView(R.layout.set_date);
		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon_time);
		
		btnSetDate			= (Button)findViewById(R.id.SetDate);
		btnCancelDate		= (Button)findViewById(R.id.CancelDate);
		
		btnCongDate 		= (Button)findViewById(R.id.btnCongDate);
		btnTruDate			= (Button)findViewById(R.id.btnTruDate);
		btnCongMonth		= (Button)findViewById(R.id.btnCongMonth);
		btnTruMonth			= (Button)findViewById(R.id.btnTruMonth);
		btnCongYear			= (Button)findViewById(R.id.btnCongYear);
		btnTruYear			= (Button)findViewById(R.id.btnTruYear);
		
		editDate			= (EditText)findViewById(R.id.editDate);
		editMonth			= (EditText)findViewById(R.id.editMonth);
		editYear			= (EditText)findViewById(R.id.editYear);
		
		if(GlobalVariable.DATE_CURRENT<10)
		{
			date = "0"+String.valueOf(GlobalVariable.DATE_CURRENT);
		}
		else
		{
			date = String.valueOf(GlobalVariable.DATE_CURRENT);
		}
		if(GlobalVariable.MONTH_CURRENT<10)
		{
			month = "0"+String.valueOf(GlobalVariable.MONTH_CURRENT);
		}
		else
		{
			month = String.valueOf(GlobalVariable.MONTH_CURRENT);
		}
		
		editDate.setText(date);
		editMonth.setText(month);
		editYear.setText(String.valueOf(GlobalVariable.YEAR_CURRENT));
		
		btnSetDate.setOnClickListener(this);
		btnCancelDate.setOnClickListener(this);
		
		btnCongDate.setOnClickListener(this);
		btnTruDate.setOnClickListener(this);
		btnCongMonth.setOnClickListener(this);
		btnTruMonth.setOnClickListener(this);
		btnCongYear.setOnClickListener(this);
		btnTruYear.setOnClickListener(this);
		
		mContext = this;
		
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnCongDate:
			GlobalVariable.DATE_CURRENT = Integer.parseInt(editDate.getText().toString());
			GlobalVariable.DATE_CURRENT ++;
			
			if(GlobalVariable.MONTH_CURRENT==1||GlobalVariable.MONTH_CURRENT==3||
					GlobalVariable.MONTH_CURRENT==5||GlobalVariable.MONTH_CURRENT==7
					||GlobalVariable.MONTH_CURRENT==8||GlobalVariable.MONTH_CURRENT==10||
					GlobalVariable.MONTH_CURRENT==12)
			{
				if(GlobalVariable.DATE_CURRENT>31)
				{
					GlobalVariable.DATE_CURRENT = 1;
				}
			}
			else
			{
				if(GlobalVariable.MONTH_CURRENT==2)
				{
					if(GlobalVariable.DATE_CURRENT>28)
					{
						GlobalVariable.DATE_CURRENT = 1;
					}
				}
				else
				{
					if(GlobalVariable.DATE_CURRENT>30)
					{
						GlobalVariable.DATE_CURRENT = 1;
					}
				}
			}
			if(GlobalVariable.DATE_CURRENT<10)
			{
				editDate.setText("0" + String.valueOf(GlobalVariable.DATE_CURRENT));
			}
			else
			{
				editDate.setText(String.valueOf(GlobalVariable.DATE_CURRENT));
			}
			
			break;
		case R.id.btnTruDate:
			GlobalVariable.DATE_CURRENT = Integer.parseInt(editDate.getText().toString());
				GlobalVariable.DATE_CURRENT --;
			
			if(GlobalVariable.MONTH_CURRENT==1||GlobalVariable.MONTH_CURRENT==3||
					GlobalVariable.MONTH_CURRENT==5||GlobalVariable.MONTH_CURRENT==7
					||GlobalVariable.MONTH_CURRENT==8||GlobalVariable.MONTH_CURRENT==10||
					GlobalVariable.MONTH_CURRENT==12)
			{
				if(GlobalVariable.DATE_CURRENT<=1)
				{
					GlobalVariable.DATE_CURRENT = 31;
				}
			}
			else
			{
				if(GlobalVariable.MONTH_CURRENT==2)
				{
					if(GlobalVariable.DATE_CURRENT<=1)
					{
						GlobalVariable.DATE_CURRENT = 28;
					}
				}
				else
				{
					if(GlobalVariable.DATE_CURRENT<=1)
					{
						GlobalVariable.DATE_CURRENT = 30;
					}
				}
			}
			if(GlobalVariable.DATE_CURRENT<10)
			{
				editDate.setText("0" + String.valueOf(GlobalVariable.DATE_CURRENT));
			}
			else
			{
				editDate.setText(String.valueOf(GlobalVariable.DATE_CURRENT));
			}
			
			
			break;
		case R.id.btnCongMonth:
			GlobalVariable.MONTH_CURRENT = Integer.parseInt(editMonth.getText().toString());
			GlobalVariable.MONTH_CURRENT ++;
			
			if(GlobalVariable.MONTH_CURRENT>12)
			{
				GlobalVariable.MONTH_CURRENT = 1;
			}
			if(GlobalVariable.MONTH_CURRENT<10)
			{
				editMonth.setText("0" + String.valueOf(GlobalVariable.MONTH_CURRENT));
			}
			else
			{
				editMonth.setText(String.valueOf(GlobalVariable.MONTH_CURRENT));
			}
			break;
		case R.id.btnTruMonth:
			GlobalVariable.MONTH_CURRENT = Integer.parseInt(editMonth.getText().toString());
			GlobalVariable.MONTH_CURRENT --;
			if(GlobalVariable.MONTH_CURRENT<=1)
			{
				GlobalVariable.MONTH_CURRENT = 12;
			}
			if(GlobalVariable.MONTH_CURRENT<10)
			{
				editMonth.setText("0" + String.valueOf(GlobalVariable.MONTH_CURRENT));
			}
			else
			{
				editMonth.setText(String.valueOf(GlobalVariable.MONTH_CURRENT));
			}
			break;
		case R.id.btnCongYear:
			GlobalVariable.YEAR_CURRENT = Integer.parseInt(editYear.getText().toString());
			GlobalVariable.YEAR_CURRENT ++;
			editYear.setText(String.valueOf(GlobalVariable.YEAR_CURRENT));
			break;
		case R.id.btnTruYear:
			GlobalVariable.YEAR_CURRENT --;
			editYear.setText(String.valueOf(GlobalVariable.YEAR_CURRENT));
			break;
		case R.id.SetDate:
			GlobalVariable.DATE = Integer.parseInt(editDate.getText().toString());
			GlobalVariable.MONTH = Integer.parseInt(editMonth.getText().toString());;
			GlobalVariable.YEAR = Integer.parseInt(editYear.getText().toString());
			GlobalVariable.STARTDATE = true;
			finish();
			break;
		case R.id.CancelDate:
			finish();

		default:
			break;
		}
		
	}

}
